//************************************************************
//    > File Name: remove-duplicates-from-sorted-array.cpp
//    > Author: davis
//    > Mail:  
//    > Created Time: 2017年03月22日 15:39:57
//***********************************************************
//lintcode #100
#include<bits/stdc++.h>
using namespace std;
int removeDuplicates(vector<int>& nums){
	if(nums.size()<1) return 0;
	vector<int>::iterator i,j;
	for(i=j=nums.begin();++j!=nums.end();){
		if(*i != *j)
			*++i=*j;
	}	
	nums.erase(++i,j);
	return nums.size();
}
int removeDuplicates2(vector<int>& nums){
	if(nums.empty()) return 0;
	int left=0;
	int right=1;
	while(right<nums.size()){
		if(nums[left] != nums[right])
			nums[++left]=nums[right];
		++right;
	}
	nums.erase(nums.begin()+left+1,nums.end());
	return nums.size();
}
int main(){
	vector<int> a={
		1,1,2,2,2,3,4
	};
	for(auto it=a.begin();it!=a.end();++it)
		cout<<*it<<" ";
	cout<<endl;

//	int r=removeDuplicates(a);
	int r=removeDuplicates2(a);
	cout<<r<<endl;
	for(auto& c : a)
		cout<<c<<" ";
	cout<<endl;
}

